//
//  GameObject.m
//  TestFistGame
//
//  Created by  on 5/17/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "GameObject.h"
#import "GameLayer.h"
#import "iCoreGUIController.h"

@implementation GameObject

@synthesize spriteBody, handler;

-(id)initGameObjectWith:(GameLayer *)_handler {
    self = [super init];
    handler = _handler;
    spriteBody = [[CCSprite alloc]init];
    
    PI = 3.14159;
    
    return self;
}

-(void)update:(ccTime)_timeUpdate {
    //NSLog(@"game object abstraction Update - timeUpdate: %f", _timeUpdate);
}

-(NSMutableArray*)loadAnimationWithNumberOfFrames:(int) numberOfFrames withFrameName:(NSString*)frame{
    NSMutableArray *anim=[[NSMutableArray alloc]init];
    for (int i=1;i<=numberOfFrames;i++)
    {
        NSString *frameName=[[NSString alloc]init];
        if(i<10)
            frameName=[NSString stringWithFormat:@"%@_0%d.png",frame,i];
        else
            frameName=[NSString stringWithFormat:@"%@_%d.png",frame,i];
        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
        [anim addObject:frame];
    }
    return anim;
}

-(CCAnimation *)loadAnimationFromCacheByName:(NSString *)_name withCount:(int)_frameCount delay:(float)_delay {
    CCAnimation *result = [[CCAnimationCache sharedAnimationCache] animationByName:_name];
    
    if(result == nil) {
        NSMutableArray *listFrameAnimation = [self loadAnimationWithNumberOfFrames:_frameCount withFrameName:_name];
        result = [CCAnimation animationWithFrames:listFrameAnimation delay:_delay];
        
        [[CCAnimationCache sharedAnimationCache] addAnimation:result name:_name];
        
        [listFrameAnimation removeAllObjects];
        [listFrameAnimation release];
    }
    
    return result;
}

-(BOOL)removeAnimationInFrameCache:(NSString *)_frameCacheName {
    return [handler.mainGUIController removeAnimationInFrameCache:_frameCacheName];
}

-(float)vectorToRadians:(CGPoint)vector{
	if(vector.y == 0){ vector.y = 0.000001f; }
	float baseRadians = atan(vector.x/vector.y);
	if(vector.y < 0){ baseRadians += PI; }	//Adjust for -Y
	return baseRadians;
}

-(CGPoint)radiansToVector:(float)radians{
	return ccp(sin(radians-PI/2), cos(radians-PI/2));
}

-(float)radiansToDegrees:(float)r{
	return r * (180/PI);
}

-(CGPoint)roundPoint:(CGPoint)_point{
    return CGPointMake(roundf(_point.x),roundf(_point.y));
}

-(float)degreesToRadians:(float)d{
	return d * (PI/180);
}

- (CGRect) rect {
	float scaleMod = 0.5f;
	float w = spriteBody.contentSize.width * spriteBody.scale * scaleMod;
	float h = spriteBody.contentSize.height * spriteBody.scale * scaleMod;
	CGPoint point = CGPointMake(spriteBody.position.x - (w/2), spriteBody.position.y - (h/2));
	return CGRectMake(point.x, point.y, w, h); 
}

-(void)dealloc {
    NSLog(@"object dealloc");
    [spriteBody stopAllActions];
    [spriteBody removeAllChildrenWithCleanup:YES];
    [spriteBody removeFromParentAndCleanup:YES];
    [spriteBody release];
    
    [self stopAllActions];
    [self removeAllChildrenWithCleanup:YES];
    [self removeFromParentAndCleanup:YES];
    
    [super dealloc];
}

@end
